package org.irri.smta.ui.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.irri.smta.common.Constants;
import org.irri.smta.domain.ReportLog;
import org.irri.smta.service.IReportLogService;
import org.irri.smta.util.BeanUtils;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@Named("reportLogBean")
@SessionScoped
public class ReportLogBean extends SelectableTableBeanBase<ReportLog> {

	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ReportLogBean.class);

	@Inject
	@Named("reportLogService")
	private IReportLogService<ReportLog, Long> reportLogService;

	private LazyDataModel<ReportLog> reportLogDataModel = null;
	private ReportLog selectedReportLog = new ReportLog();
	private BeanUtils beanUtils = new BeanUtils();
	
	public ReportLogBean() {
		super();
		setViewName(Constants.REPORT_LOG_VIEW_NAME);
	}

	@PostConstruct
	public void init() {
		LOGGER.debug("init()");
		selectedReportLog = new ReportLog();
	}

	public IReportLogService<ReportLog, Long> getReportLogService() {
		return reportLogService;
	}

	public ReportLog getSelectedReportLog() {
		if (selectedReportLog == null) {
			selectedReportLog = new ReportLog();
		}
		return selectedReportLog;
	}

	public void setSelectedReportLog(ReportLog reportLog) {
		this.selectedReportLog = reportLog;
	}

	public LazyDataModel<ReportLog> getAllReportLogs() {
		if (reportLogDataModel == null) {
			reportLogDataModel = new LazyDataModel<ReportLog>() {
				private static final long serialVersionUID = 1L;

				@Override
				public List<ReportLog> load(int rowIndex, int pageSize,
						String sortField, SortOrder sortOrder,
						Map<String, String> filters) {
					LOGGER.debug("List<ReportLog> load(int rowIndex, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)");
					List<ReportLog> data = new ArrayList<ReportLog>();
					if (rowIndex < 0) {
						rowIndex = 0;
					}
					if (pageSize <= 0) {
						pageSize = Constants.DEFAULT_PAGE_SIZE;
					}

					int pageIndex = rowIndex / pageSize;
					Pageable pageable = new PageRequest(pageIndex, pageSize,
							new Sort(Sort.Direction.DESC, "createdDate"));
					List<ReportLog> list = reportLogService.findAll(pageable);
					data.addAll(list);

					// rowCount
					Long rowCount = reportLogService.getRowCount();
					Integer dataSize = rowCount != null ? rowCount.intValue()
							: null;
					this.setRowCount(dataSize);
					// pageSize
					this.setPageSize(pageSize);

					return data;
				}
			};
		}
		return reportLogDataModel;
	}

	@Override
	public Object getRowKey(ReportLog object) {
		return object.getId();
	}

	@Override
	public ReportLog getRowData(String rowKey) {
		return reportLogService.findById(new Long(rowKey));
	}
	
	public void onComplete() {
		beanUtils.updateInfoMessage("Completed.", "Report Logs deleted!");
	}
	
	public void delete() {
		reportLogService.deleteReportLog();
		onComplete();
	}

}
